package com.employ.job.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.employ.common.utils.StringUtils;
import com.employ.job.domain.EmpResumes;
import com.employ.job.service.IEmpResumesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.employ.common.annotation.Log;
import com.employ.common.core.controller.BaseController;
import com.employ.common.core.domain.AjaxResult;
import com.employ.common.enums.BusinessType;
import com.employ.job.domain.EmpStudents;
import com.employ.job.service.IEmpStudentsService;
import com.employ.common.utils.poi.ExcelUtil;
import com.employ.common.core.page.TableDataInfo;

/**
 * 学生Controller
 *
 * @author ruoyi
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/job/students")
public class EmpStudentsController extends BaseController
{
    @Autowired
    private IEmpStudentsService empStudentsService;

    @Autowired
    private IEmpResumesService empResumesService;

    /**
     * 查询学生列表
     */
    @PreAuthorize("@ss.hasPermi('job:students:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpStudents empStudents)
    {
        startPage();
        List<EmpStudents> list = empStudentsService.selectEmpStudentsList(empStudents);
        return getDataTable(list);
    }

    /**
     * 导出学生列表
     */
    @PreAuthorize("@ss.hasPermi('job:students:export')")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpStudents empStudents)
    {
        List<EmpStudents> list = empStudentsService.selectEmpStudentsList(empStudents);
        ExcelUtil<EmpStudents> util = new ExcelUtil<EmpStudents>(EmpStudents.class);
        util.exportExcel(response, list, "学生数据");
    }

    /**
     * 获取学生详细信息
     */
    @PreAuthorize("@ss.hasPermi('job:students:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(empStudentsService.selectEmpStudentsByUserId(userId));
    }

    /**
     * 新增学生
     */
    @PreAuthorize("@ss.hasPermi('job:students:add')")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpStudents empStudents)
    {
        Long userId = getUserId();
        EmpStudents empStudentsCount = empStudentsService.selectEmpStudentsByUserId(userId);
        if (empStudentsCount != null){
            return error("该学生信息已存在！");
        }

        // 写入简历表
        if (StringUtils.isNotEmpty(empStudents.getFilePath())){
            EmpResumes empResumes = new EmpResumes();
            empResumes.setStudentId(getUserId());
            empResumes.setFilePath(empStudents.getFilePath());
            empResumesService.insertEmpResumes(empResumes);
        }
        empStudents.setUserId(userId);
        return toAjax(empStudentsService.insertEmpStudents(empStudents));
    }

    /**
     * 修改学生
     */
    @PreAuthorize("@ss.hasPermi('job:students:edit')")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpStudents empStudents)
    {
        // 写入简历表
        if (StringUtils.isNotEmpty(empStudents.getFilePath())){

            EmpResumes empResumes = new EmpResumes();
            String filePath = empStudents.getFilePath();
            // 获取最后一个 '/' 后面的所有内容
            String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
            // 获取 '_' 前面的部分
            String result = fileName.substring(0, fileName.indexOf("_"));

            empResumes.setContent(result);
            empResumes.setStudentId(getUserId());
            empResumes.setFilePath(empStudents.getFilePath());

            EmpResumes empResumesCount = empResumesService.selectEmpByUserId(getUserId());
            if (empResumesCount != null){
                empResumes.setId(empResumesCount.getId());
                empResumesService.updateEmpResumes(empResumes);
            }else{
                empResumesService.insertEmpResumes(empResumes);
            }
        }
        return toAjax(empStudentsService.updateEmpStudents(empStudents));
    }

    /**
     * 删除学生
     */
    @PreAuthorize("@ss.hasPermi('job:students:remove')")
    @Log(title = "学生", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(empStudentsService.deleteEmpStudentsByUserIds(userIds));
    }
}
